package com.example.news.activity;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.news.R;
import com.example.news.helper.UserManage;
import com.example.news.helper.gethttp;

class SignDialog extends Dialog {
   public EditText userid;
   public EditText username;
   public EditText userpasswd;
   public EditText userpasswd2;
   public Button sign;
   public SignDialog(@NonNull Context context, int themeResId, String ip, Handler signhandler) {
      super(context, themeResId);
      setContentView(R.layout.dialog_sign);
      userid=findViewById(R.id.userid);
      username=findViewById(R.id.username);
      userpasswd=findViewById(R.id.passwd);
      userpasswd2=findViewById(R.id.passwd2);
      sign=findViewById(R.id.btn_sign);
      sign.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            String pw1=userpasswd.getText().toString();
            String pw2=userpasswd2.getText().toString();
            if(pw1.equals(pw2)) {
               UserManage.getInstance().saveUserInfo(context, userid.getText().toString(), userpasswd.getText().toString(),"0",-1);
               String url = "http://" + ip + ":8080/user/sign?userId=" + userid.getText() + "&userPasswd=" + userpasswd.getText() + "&userName=" + username.getText() + "&userType=2";
               gethttp ghttp = new gethttp(url, signhandler);
               ghttp.start();
               try {
                  ghttp.join();
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
            }else{
               Toast.makeText(context,"两次密码不一致",Toast.LENGTH_SHORT).show();
            }
         }
      });
   }
}
